package cn.edu.nuc.bre.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.nuc.bre.service.interfaces.GroupService;

/**
 * @author Mine
 * @since 2016年11月10日_下午1:09:23
 */
@Service
public class GroupServiceImpl implements GroupService {
	@Autowired
	private IdentityService is;

	@Override
	public boolean add(Group one) {
		if (one != null) {
			if (is.createGroupQuery().groupId(one.getId()).singleResult() == null) {
				is.newGroup(one.getId());// 要显式写一下
				is.saveGroup(one);
				return true;
			} else {
				System.out.println("角色已存在...");
			}
		} else {
			System.out.println("参数为空...");
		}
		return false;
	}

	@Override
	public boolean edit(Group one) {
		if (one != null) {
			Group temp = is.createGroupQuery().groupId(one.getId()).singleResult();
			if (temp != null) {
				temp.setName(one.getName());
				// temp.setType(one.getType()); //不要修改
				is.saveGroup(temp);// 直接修改
				return true;
			} else {
				System.out.println("角色不存在...");
			}
		} else {
			System.out.println("参数为空...");
		}
		return false;
	}

	@Transactional(rollbackFor = { Exception.class })
	@Override
	public boolean delete(String gid) {
		boolean flag = false;
		try {
			// 删除旧关系
			List<User> users = this.findUsersById(gid);
			if (users != null) {
				for (User user : users) {
					is.deleteMembership(user.getId(), gid);
				}
			}
			is.deleteGroup(gid);
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException("异常了...");
		}
		return flag;
	}

	@Override
	public Group findById(String gid) {
		return is.createGroupQuery().groupId(gid).singleResult();
	}

	@Override
	public List<Group> findByTypes(String... types) {
		List<Group> groups = null;
		if (types != null) {
			groups = new ArrayList<Group>();
			for (String type : types) {// 循环放入
				groups.addAll(is.createGroupQuery().groupType(type).list());
			}
		}
		return groups;
	}

	@Override
	public List<User> findUsersById(String gid) {
		if (gid != null) {
			return is.createUserQuery().memberOfGroup(gid).list();
		} else {
			return null;
		}
	}

	@Override
	public Map<String, List<Group>> findGroupsMapByTypes(String... types) {
		Map<String, List<Group>> groupsMap = null;
		if (types == null) {
			return null;
		}
		groupsMap = new HashMap<String, List<Group>>();
		List<Group> temps = null;
		for (String type : types) {// 循环放入
			temps = is.createGroupQuery().groupType(type).list();
			if (temps != null) {
				groupsMap.put(type, temps);
			}
		}
		if (groupsMap.size() < 1) {
			return null;
		}
		return groupsMap;
	}

	@Override
	public Group findByName(String gname) {
		return is.createGroupQuery().groupName(gname).singleResult();
	}
}
